#include "StdAfx.h"
#include "HSceneConfig.h"
#include "HCommonDefination.h"
#include "HGlobalManager.h"

namespace BombBox
{
	wchar_t buffer[255];
	wchar_t* Slot::getStr()
	{
		swprintf(buffer, L"%d,%d,%d,%d", nX,nY,nZ,nType);
		return buffer;
	}

	bool Slot::fromStr( const char* pData )
	{
		if(4==sscanf_s(pData,"%d,%d,%d,%d", &nX,&nY,&nZ,&nType))
			return true;

		return false;
	}



	SceneConfig::SceneConfig(void)
	{
		m_bPublished = false;
		m_vBombsData.clear();
	}

	SceneConfig::~SceneConfig(void)
	{
		//if( m_nData != NULL )
		//	delete m_nData;
	}

	void SceneConfig::saveConfig( irr::io::IXMLWriter* sFileWriter )
	{
		sFileWriter->writeElement(L"Rooms",false,L"File",irr::core::stringw(m_sMapName).c_str(),L"Publish",m_bPublished?L"true":L"false");
		sFileWriter->writeLineBreak();
		int nSize = m_vDatas.size();
		for( int i=0; i<nSize; ++i )
		{
			sFileWriter->writeElement(L"Boxes", true, L"Info", m_vDatas[i].getStr());
			sFileWriter->writeLineBreak();
		}

		// Bombs
		int nBombCnt = getBombCnt( 401 );
		sFileWriter->writeElement(L"Bombs", true, L"ID", L"401", L"Num", irr::core::stringw(nBombCnt).c_str() );
		sFileWriter->writeLineBreak();
		nBombCnt = getBombCnt( 402 );
		sFileWriter->writeElement(L"Bombs", true, L"ID", L"402", L"Num", irr::core::stringw(nBombCnt).c_str() );
		sFileWriter->writeLineBreak();
		nBombCnt = getBombCnt( 403 );
		sFileWriter->writeElement(L"Bombs", true, L"ID", L"403", L"Num", irr::core::stringw(nBombCnt).c_str() );
		sFileWriter->writeLineBreak();
		nBombCnt = getBombCnt( 404 );
		sFileWriter->writeElement(L"Bombs", true, L"ID", L"404", L"Num", irr::core::stringw(nBombCnt).c_str() );
		sFileWriter->writeLineBreak();

		sFileWriter->writeClosingTag(L"Rooms");
		sFileWriter->writeLineBreak();
	}

	void SceneConfig::loadConfig( irr::core::stringc sFile )
	{
		clear();

		irr::io::IFileSystem* fileSys = s_pGlobalManager->getFileSystem();
		irr::io::IXMLReader* pReader = fileSys->createXMLReader( sFile );

		while(pReader && pReader->read())
		{
			switch(pReader->getNodeType())
			{
			case irr::io::EXN_ELEMENT:
				{
					if(irr::core::stringw("Config") == pReader->getNodeName())
					{
						irr::core::stringc bPublish = pReader->getAttributeValue(L"Publish");
						if( bPublish == irr::core::stringc("true") )
							m_bPublished = true;
						else
							m_bPublished = false;
					}

					if(irr::core::stringw("Rooms") == pReader->getNodeName())
					{
						irr::core::stringc sRoomsFile = pReader->getAttributeValue(L"File");
						m_sMapName = sRoomsFile;
					}

					if (irr::core::stringw("Boxes") == pReader->getNodeName())
					{
						irr::core::stringc sBoxInfo = pReader->getAttributeValue(L"Info");
						Slot slot;
						if( slot.fromStr( sBoxInfo.c_str() ) )
						{
							m_vDatas.push_back(slot);
						}
					}

					if (irr::core::stringw("Bombs") == pReader->getNodeName())
					{
						int nBombID = pReader->getAttributeValueAsInt(L"ID");
						int nCount = pReader->getAttributeValueAsInt(L"Num");
						m_vBombsData[nBombID] = nCount;
					}
				}
			}
		}
	}

	void SceneConfig::setPublish( void )
	{
		m_bPublished = true;
	}

	void SceneConfig::clear( void )
	{
		m_vDatas.clear();
		m_vBoundingBox.reset(0,0,0);
		m_vBombsData.clear();
	}

	void SceneConfig::setCreater( irr::core::stringc sCreater )
	{
		m_sCreater = sCreater;
	}

	void SceneConfig::setMapName( irr::core::stringc sMapName )
	{
		m_sMapName = sMapName;
	}
	
	void SceneConfig::addData( int x, int y, int z, int data )
	{
		m_vDatas.push_back(Slot(x,y,z,data));
	}

	int SceneConfig::incBombCount( int nBombID )
	{
		int nCount = 0;
		irr::core::map<int,int>::Node* pNode = m_vBombsData.find( nBombID );
		if( pNode )
		{
			nCount = pNode->getValue();
		}

		nCount++;
		if( nCount > 9 ) nCount = 0;
		m_vBombsData[nBombID] = nCount;
		return nCount;
	}

	void SceneConfig::clearData( int x,int y,int z )
	{
		int nSize = m_vDatas.size();
		for( int i=0; i<nSize; ++i )
		{
			Slot& slot = m_vDatas[i];
			if( slot.isSlot(x,y,z) )
			{
				m_vDatas.erase(i);
				return;
			}
		}
	}

	int SceneConfig::getBombCnt( int nBombID )
	{
		if( m_vBombsData.find( nBombID ) == NULL ) return 0;
		return m_vBombsData[nBombID];
	} 
	
	irr::core::stringc SceneConfig::getCreater( void )
	{
		return m_sCreater;
	}
	
	irr::core::stringc SceneConfig::getMapName( void )
	{
		return m_sMapName;
	}
	
	const irr::core::array<Slot>& SceneConfig::getData( void )
	{
		return m_vDatas;
	}

	const irr::core::map<int,int>& SceneConfig::getBombData( void )
	{
		return m_vBombsData;
	}

	void SceneConfig::setDefaultRoom( void )
	{
		m_sMapName = "media/Plane.X";
	}
}